<?php

/**
 * pm2.5的类
 */
class pm25Model
{

    private $redis_key = '';

    public function __construct(& $base)
    {
        $this->base = $base;
        $this->db = & $this->base->db;
        $this->redis = & $this->base->redis;
        $this->redis_key = REDIS_PREFIX . 'pm25';
    }

    /**
     * 返回所有城市列表
     * @return array
     */
    public function getAllCity()
    {
        $runData = $this->db->getAll('select city_id,city from r_city');
        return $runData;
    }

    public function insertPm25(array $data)
    {
        //更新redis的天气数据
        $val = array_intersect_key($data, array('city_id' => '', 'pm25' => '', 'quality' => '', 'level' => ''));
        $this->redis->hset($this->redis_key, $data['city_id'], json_encode($val));

        // $insertStr = common::arrayToString($data);
        // $id = $this->base->db->insert("insert HIGH_PRIORITY into r_pm25 set {$insertStr} on duplicate key update {$insertStr}");

        // return $id;
        return;
    }

    /**
     * 获取某个城市pm2.5信息
     * @param type $city_id
     */
    public function getLevel($city_id)
    {
        $json_str = '';
        if (!$this->redis->hexists($this->redis_key, $city_id))
        {
            $city_id = substr($city_id, 0, 2) . '0100';
        }
        $json_str = $this->redis->hget($this->redis_key, $city_id);
        return json_decode($json_str);
    }

}

?>
